package com.yuelife.admin.controller;

import com.yuelife.admin.feignclient.ProductServiceClient;
import com.yuelife.admin.pojo.MCategory;
import com.yuelife.admin.pojo.MProduct;
import com.yuelife.admin.service.MProductService;
import com.yuelife.admin.util.DataGridView;
import com.yuelife.admin.vo.MCategoryVo;
import com.yuelife.admin.vo.MProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    private MProductService mProductService;
    @Autowired
    private ProductServiceClient client;

    @GetMapping("productList")
    public DataGridView productList(MProductVo mProductVo){
        if (mProductVo.getName()!=null) {
            System.out.printf(mProductVo.getName());
        }
        mProductVo.setStatus(2);
        return mProductService.selectAllProduct(mProductVo);
    }

    @PutMapping("/successProduct/{productSn}/{status}")
    public Map successProduct(@PathVariable("productSn") String productSn,@PathVariable("status") Integer status){
        Map map=new HashMap();
        MProduct mProduct=new MProduct();
        mProduct.setProductSn(productSn);
        mProduct.setStatus(status);
        try {
            MProduct m=mProductService.update(mProduct);
            //微服务调用
            client.updataRedisProduct(m);
            map.put("code",200);
            map.put("msg","操作成功");
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",500);
            map.put("msg","操作失败");
            return map;
        }
    }

    /**
     * 已通过审核的商品
     * @param mProductVo
     * @return
     */
    @GetMapping("productAll")
    public DataGridView productAll(MProductVo mProductVo){
        mProductVo.setStatus(0);
        return mProductService.selectAllProduct(mProductVo);
    }

    /**
     * 删除商品
     * @param productSn
     * @return
     */
    @DeleteMapping("/deleteProduct/{productSn}")
    public Map deleteProduct(@PathVariable("productSn") String productSn){
        Map map=new HashMap();
        try {
            mProductService.deleteProductByProductSn(productSn);
            client.deleteRedisProduct(productSn);
            map.put("code",200);
            map.put("msg","操作成功");
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",500);
            map.put("msg","操作失败");
            return map;
        }
    }
    @GetMapping("/typeList")
    public DataGridView typeList(MCategoryVo mCategoryVo){

        return mProductService.typeList(mCategoryVo);
    }

    //更改分类
    @PutMapping("/editCategory/{id}/{type}")
    public int editCategory(@PathVariable("id") Integer id,@PathVariable("type") String type){
        try {
            mProductService.editCategory(id,type);
            return 200;
        } catch (Exception e) {
            e.printStackTrace();
            return 500;
        }
    }

}
